package redis

import (
	"cart_srv/app_config"
	"context"
	"fmt"
	"github.com/go-redis/redis/v8"
)

var (
	redisClient *redis.Client
	err         error
)

func Init() {
	rc := app_config.AppConf.Redis
	redisClient = redis.NewClient(&redis.Options{
		Addr:     rc.Host + ":" + rc.Port,
		Password: rc.Pwd,
		DB:       rc.DB,
	})
	_, err = redisClient.Ping(context.Background()).Result()
	if err != nil {
		panic(err)
	}
	fmt.Println("redis 连接成功:", redisClient)
}

func ClearCart(goodsId int) bool {
	key := fmt.Sprintf("cart:goods_%d*", goodsId)
	keys := redisClient.HGetAll(context.Background(), key).Val()
	for _, v := range keys {
		redisClient.Del(context.Background(), v)
	}
	return true
}
